<?php

namespace App\Admin\Controllers;

use App\Http\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    // 文章列表页面
    public function index()
    {
        $posts = Post::query()->withoutGlobalScope('avaiable')->where('status', 0)->latest()->paginate(10);

        return view('admin.post.index', compact('posts'));
    }

    // 审核操作
    public function status(Post $post, Request $request)
    {
        $rules = [
            'status' => 'required|int|in:-1,1',
        ];
        $validator = $this->getValidationFactory()->make($request->all(), $rules);
        if ($validator->fails()) {
            return [
                'code' => -1,
                'msg' => $validator->errors()->first() . json_encode($request->getContent()),
            ];
        }
        $post->status = $request->input('status');
        $post->save();

        return ['code' => 0, 'msg' => 'success'];
    }
}
